﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using System.Data.Entity.Repository.Filters;

using Testing.DataService.Entities;

namespace Testing.IntegrationTests.Filters
{
    public interface IProductFilter
    { }

    public class ProductFilterDuvelGreen2 : IQueryableFilter, IProductFilter
    {
        public IQueryable<T> Filter<T>(IQueryable<T> profileFields, dynamic parameterBag = null)
        {
            //return profileFields
            //    .Where(x => (x as Product).ProductName != "Duvel Green 2" && (x as Product).CategoryID == 1);

            return profileFields
                .Cast<Product>()
                .Where(x => x.ProductName != "Duvel Green 2" && x.CategoryID == 1)
                .Cast<T>();
        }
    }

    public class ProductFilterNumber2 : IQueryableFilter, IProductFilter
    {
        public IQueryable<T> Filter<T>(IQueryable<T> profileFields, dynamic parameterBag = null)
        {
            return profileFields
                .Cast<Product>()
                .Where(x => x.ProductName.Contains("Duvel Green"))
                .Cast<T>();
        }
    }

    public class PagingFilter : IQueryableFilter, IGlobalQueryFilter
    {
        public IQueryable<T> Filter<T>(IQueryable<T> profileFields, dynamic parameterBag = null)
        {
            IQueryable<Product> result;

            result = profileFields
                .Cast<Product>()
                .Where(x => x.ProductName.Contains("Duvel Green"))
                .OrderBy(p => p.ProductID);

            if (parameterBag != null)
            {
                result = result.Skip((int)parameterBag.Skip);
                result = result.Take((int)parameterBag.Take);
            }

            return result.Cast<T>();
        }
    }
}
